﻿using System;
using System.Reflection;
using System.Text;

namespace CommonUtils.CodeFactory
{
    /// <summary>
    /// hack FieldType PropertyType 可否用 MemberInfo.ReflectedType 代替? 不可以 取不到
    /// </summary>
    public static class SqlUtil
    {
        /// <summary>
        /// CsvToSql
        /// </summary>
        public static string GenerateInsertScript(string csvPath, string tableName = "@tableName")
        {
            string[] lines = FileUtil.ReadLines(csvPath);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("insert into {0} values ", tableName));
            for (int index = 0; index < lines.Length; index++)
            {
                var line = lines[index];
                sb.Append('(');

                var cells = line.Split(',');
                for (int subIndex = 0; subIndex < cells.Length; subIndex++)
                {
                    var cell = cells[subIndex];
                    if (subIndex != cells.Length - 1)
                        sb.Append(string.Format("'{0}',", cell));
                    else
                        sb.Append(string.Format("'{0}'", cell));
                }

                if (index != lines.Length - 1)
                    sb.AppendLine("),");
                else
                    sb.AppendLine(");");
            }
            return sb.ToString();
        }
    }
}
